package com.goal98.team.services

import com.goal98.team.domain.Team
import com.goal98.team.domain.Person
import com.goal98.team.domain.Activity

class NotificationService {

    def emailerService
    def activityService
    def grailsTemplateEngineService

    boolean transactional = false

    def sendUpcomingActivityNotification() {
        Team.list().each {Team team ->
            sendUpcomingActivityNotification(team)
        }
    }

    def sendUpcomingActivityNotification(Team team) {
        Activity upcomingActivity = activityService.getUpcomingActivity(team)
        if (upcomingActivity) {



            def emailContent =
                grailsTemplateEngineService.renderView(
                        '/email/_activityNotification', [team:team, upcomingActivity:upcomingActivity])
            def subject = "Goal98 Team Manager -- ${team.name} 下次活动"

            team?.members?.each {Person member ->
                if (member.receiveEmail) {
                    def email = [
                            to: [member.username],
                            subject: subject,
                            text: emailContent
                    ]
                    emailerService.sendHtmlEmails([email])
                }
            }
        }
    }
}
